using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Ghasam.Business.Services;
using Mani.Common;
using Ghasam.Business.Domain.Entity;

public partial class EmployeePage : BasePage
{
    #region"Private Property"

    private IEmployeeService employeeService;

    #endregion
    #region"Public Methods"
    public override string ActionName
    {
        get
        {
            return "vw_newemployee";
        }
    }
    public IEmployeeService EmployeeService
    {
        set { employeeService = value; }
        get { return employeeService; }
    }

    #endregion
    #region"Private Method"

    private void fillJobTitleList()
    {
        ddlJobTitle.DataSource = EmployeeService.FindAllJobTitle();
        ddlJobTitle.DataTextField = "Title";
        ddlJobTitle.DataValueField = "Id";
        ddlJobTitle.DataBind();

    }
    private void fillPaymentFreqeuncyList()
    {
        ddlPaymentF.DataSource = EmployeeService.FindAllPaymentFrequency();
        ddlPaymentF.DataTextField = "Frequently";
        ddlPaymentF.DataValueField = "Id";
        ddlPaymentF.DataBind();

    }
    private void fillEducationList()
    {
        ddlEducation.DataSource = EmployeeService.FindAllEducation();
        ddlEducation.DataTextField = "Description";
        ddlEducation.DataValueField = "Id";
        ddlEducation.DataBind();
    }
    private void Save()
    {
        bool marrid, gender;
        int empId = 0;
        empId =Convert.ToInt32(lblEmpId.Text);
        marrid = (ddlMarreid.SelectedValue != "0");
        gender = (ddlGender.SelectedValue != "0");
        float exteraPay = 0;
        if (txtExteraPay.Text.Length > 0) exteraPay = Convert.ToSingle(txtExteraPay.Text);
        lblEmpId.Text = EmployeeService.EmployeeSaveOrUpadate(empId,Convert.ToInt32(ddlEducation.SelectedValue), Convert.ToInt32(ddlPaymentF.SelectedValue), Convert.ToInt32(ddlJobTitle.SelectedValue), MyPersianDate.PersianToDateTime(txtBirthday.Text),
                                 MyPersianDate.PersianToDateTime(txtJoinedDate.Text), null, txtName.Text, txtLastName.Text,
                                 txtAddress.Text, txtPhone.Text, txtSsn.Text, txtBornPlace.Text, txtIdentityNo.Text, gender, chkInssurance.Checked, marrid,
                                 chkIsFormal.Checked, true, false, txtDesc.Text, exteraPay, txtInsuranceNo.Text, DateTime.Now.Date).Id.ToString();
    }
    private void clearData()
    {
        txtBirthday.Text = "";
        txtExteraPay.Text = "";
        txtDesc.Text = "";
        txtPhone.Text = "";
        txtAddress.Text = "";
        txtBornPlace.Text = "";
        txtIdentityNo.Text = "";
        txtName.Text = "";
        txtLastName.Text = "";
        txtJoinedDate.Text = "";
        chkIsFormal.Checked = false;
        chkInssurance.Checked = false;
        ddlEducation.ClearSelection();
        ddlMarreid.ClearSelection();
        ddlJobTitle.ClearSelection();
        txtSsn.Text = "";
        txtInsuranceNo.Text = "";
        chkIsActive.Checked = false;
        lblEmpId.Text = "0";
        lblSaveMsg.Visible = false;
        HttpContext cx = HttpContext.Current;
        string oldpath = cx.Request.Path.ToLower();
        Response.Redirect(oldpath);
        //string newpath= oldpath.Substring(oldpath.IndexOf("?"), oldpath.IndexOf(".aspx"));
        //Server.Transfer(Page.Request.RawUrl);
        

    }
    private void setEditControlValue(int empId)
    {
        Employee emp = EmployeeService.GetById(empId);
        lblEmpId.Text = emp.Id.ToString();
        txtName.Text = emp.FirstName;
        txtLastName.Text = emp.LastName;
        chkInssurance.Checked = emp.Insurance;
        chkIsActive.Checked = emp.IsActive;
        chkIsFormal.Checked = emp.IsFormal;
        ddlJobTitle.SelectedValue = emp.JobTitle.Id.ToString();
        txtJoinedDate.Text=Mani.Common.MyPersianDate.DateTimeToPersian(emp.JoinedDate);
        ddlEducation.SelectedValue =emp.Education.Id.ToString();
        txtExteraPay.Text = emp.ExtraPay.ToString();
        txtBornPlace.Text=emp.BornPlace;
        txtAddress.Text =emp.Address;
        txtPhone.Text =emp.Phone;
        txtSsn.Text=emp.SSN;
        txtDesc.Text = emp.Description;
        txtInsuranceNo.Text = emp.InsuranceNo;
        txtBirthday.Text = Mani.Common.MyPersianDate.DateTimeToPersian(emp.BirthDay);
        txtIdentityNo.Text=emp.IdentityNumber;
        ddlPaymentF.SelectedValue = emp.EmployeePaymentFrequency.Id.ToString();
        ddlMarreid.SelectedValue =(Convert.ToInt32(emp.Married)).ToString();
        ddlGender.SelectedValue = (Convert.ToInt32(emp.Gender)).ToString();
        txtLeftDate.Enabled = true;
        txtLeftDate.Text =Mani.Common.MyPersianDate.DateTimeToPersian(emp.LeftDate) ;

    }
    #endregion
    #region"Events"
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Request.QueryString["Id"] != null)
        {
            setEditControlValue(Convert.ToInt32(Request.QueryString["Id"]));
            //Request.QueryString.Remove(Request.QueryString.Keys[0]);
        }
        
        if (!IsPostBack)
        {
            fillJobTitleList();
            fillEducationList();
            fillPaymentFreqeuncyList();
        }
    }
    protected void btnSave_Click(object sender, EventArgs e)
    {
        try
        {
            Save();
            lblSaveMsg.Visible = true;
        }
        catch (Exception ex)
        { throw ex; }
    }
    protected void btnNew_Click(object sender, EventArgs e)
    {
        clearData();
    }
    #endregion
}
